<!doctype html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>AES TEST</title>
</head>
<body>
AES 注解加解密
</br>
</br>
<button id="select1" onclick="saveToken()">生成URL-token</button>
</br>
</br>
<input type="text" id="url" flex-box="1"></input>
</br>
</br>
<button id="select2" onclick="save()">加密请求</button>
</br>
</br>
<button id="select3" onclick="encrypt()">解密请求</button>
</body>
<script th:src="@{http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js}"></script>
<script type="text/javascript" th:src=@{/js/aes.js}></script>
<script type="text/javascript" th:src=@{/js/random.js}></script>
<script type="text/javascript" th:src=@{/js/security.js}></script>
<script type="text/javascript">


    function getUrlPara(para) {
        var url = location.search, arr = {};
        if (url.indexOf('?') != -1) {
            var code = url.substr(1).split('&');
            for (var i = 0; i < code.length; i++) {
                var item = code[i].split('=');
                arr[item[0]] = item[1];
            }
            return arr[para];
        }
    }


    function saveToken() {
        $.ajax({
            type: "GET",
            url: "/token",
            data: '',
            success: function (result) {
                $('#url').val(result.url);
                localStorage['codeKey'] = result.codeKey;
                localStorage['token'] = result.token;
                console.log("返回的数据：" + result.token);
            }
        });
    }

    function save() {
        var token = getUrlPara("token");
        console.log("获取token：" + token);

        var codeKey = localStorage.codeKey;
        console.log("获取加解密key：" + codeKey);
        //参数封装
        var params =
            '{"name":"测试","age":"18","ads":"我是中文","adasd":"我是特殊字符!@#$%^&*","age":"18","age":"18","age":"18","age":"18","age":"18","age":"18","age":"18","age":"18","age":"18"}';
        $.ajax({
            type: 'POST',
            url: '/annotaSave',
            data: encodeURIComponent(Encrypt(params, codeKey)),
            success: function (result) {
                if (result != null) {
                    var data = Decrypt(result, codeKey);
                    alert("数据返回成功！" + data);
                }
            }
        }).done(function (json, textStatus, jqXHR) {
            //
        }).fail(function (jqXHR, textStatus, errorThrown) {
            //
        });
    }

    function encrypt() {
        var token = getUrlPara("token");
        var codeKey = localStorage.codeKey;
        $.ajax({
            type: 'POST',
            url: '/annotaEncrypt',
            data: token,
            success: function (result) {
                var datas = Decrypt(result, codeKey);
                alert("解密数据：" + datas);
            }
        }).done(function (json, textStatus, jqXHR) {
            //
        }).fail(function (jqXHR, textStatus, errorThrown) {
            //
        });
    }
</script>

</html>
